/*
* PROJECT: Happy New Year
* VERSION: 1.0
* AUTHOR: Mattias Thell
* URL: http://www.mattiasthell.com
*
* */

#include "Settings.hpp"

#include <fstream>

namespace hny {

	Settings* Settings::mInstance = 0;

	Settings::Settings() 
		:StartingNumberRockets		(10),
		SystemScreenWidth			(1024),
		SystemScreenHeight			(768),
		SystemColorDepth			(32),
		ExplosionAlphaStart			(0.2f),
		RocketSpawnDelay			(1.0f),
		BonusRocketSpawnDelay		(3.0f),
		DualRocketSpawnDelay		(9.0f),
		RocketVelocity				(50.0f),
		BonusRocketVelocity			(100.0f),
		PlayerRocketVelocity		(200.0f),
		DualRocketVelocity			(150.0f),
		ShowCombosAfterAmount		(3),
		RocketScore					(100),
		BonusRocketScore			(200),
		DualRocketScore				(1000),
		StartingTime				(60.0f),
		TimeWarning1				(30.0f),
		TimeWarning2				(10.0f),
		MultiplierHighlight1		(200),
		MultiplierHighlight2		(300),
		RocketsRemainingWarning1	(5),
		RocketsRemainingWarning2	(2),
		MultiplierAddition			(0.1f),
		MultiplierDecreasePerSecond	(0.05f),
		MultiplierRocketFirePenalty	(0.2f),
		MultiplierPenaltyMin		(0.01f),
		ScoreFile					("Scores.hny"),
		SettingsFile				("Settings.hny"),
		ShowSplash					(true),
		SplashTime					(1.5f),
		VolumeMusic					(0.5f),
		VolumeSound					(0.5f),

		RocketColorStart			(hgeColor(0.1f, 0.0f, 1.0f, 1.0f)),
		RocketColorEnd				(hgeColor(0.5f, 0.5f, 1.0f, 0.1f)),

		PlayerRocketColorStart		(hgeColor(1.0f, 0.2f, 0.0f, 1.0f)),
		PlayerRocketColorEnd		(hgeColor(0.7f, 0.7f, 1.0f, 0.0f)),

		BonusRocketColorStart		(hgeColor(0.1f, 0.9f, 0.3f, 1.0f)),
		BonusRocketColorEnd			(hgeColor(0.6f, 0.9f, 0.1f, 1.0f)),

		DualRocketColorStart		(hgeColor(1.0f, 0.2f, 0.2f, 1.0f)),
		DualRocketColorEnd			(hgeColor(1.0f, 0.5f, 0.2f, 1.0f))
	{
	}

	Settings::~Settings() {}

	Settings* Settings::GetInstance() {
		
		if(mInstance == 0)
			mInstance = new Settings();

		return mInstance;
	}

	void Settings::Destroy() {
		if(mInstance != 0) {
			delete mInstance;
			mInstance = 0;
		}
	}

	void Settings::LoadSettings()
	{
		std::ifstream file(SettingsFile.c_str());

		if(file.is_open()) {
			
			std::string str;

			file >> str >> StartingNumberRockets;
			file >> str >> SystemScreenWidth;
			file >> str >> SystemScreenHeight;
			file >> str >> SystemColorDepth;
			file >> str >> ExplosionAlphaStart;
			file >> str >> RocketSpawnDelay;
			file >> str >> BonusRocketSpawnDelay;
			file >> str >> DualRocketSpawnDelay;
			file >> str >> RocketVelocity;
			file >> str >> BonusRocketVelocity;
			file >> str >> DualRocketVelocity;
			file >> str >> ShowCombosAfterAmount;
			file >> str >> RocketScore;
			file >> str >> BonusRocketScore;
			file >> str >> DualRocketScore;
			file >> str >> StartingTime;
			file >> str >> TimeWarning1;
			file >> str >> TimeWarning2;
			file >> str >> MultiplierHighlight1;
			file >> str >> MultiplierHighlight2;
			file >> str >> RocketsRemainingWarning1;
			file >> str >> RocketsRemainingWarning2;
			file >> str >> MultiplierAddition;
			file >> str >> MultiplierDecreasePerSecond;
			file >> str >> MultiplierRocketFirePenalty;
			file >> str >> MultiplierPenaltyMin;
			file >> str >> ScoreFile;
			file >> str >> SettingsFile;
			file >> str >> ShowSplash;
			file >> str >> SplashTime;
			file >> str >> VolumeMusic;
			file >> str >> VolumeSound;
		}
		else { // No Settings file exists, create a default one
			SaveSettings();
		}
	}

	void Settings::SaveSettings()
	{
		std::ofstream file(SettingsFile.c_str());
		
		if( file.is_open() ) {

			file << "StartingNumberRockets " << StartingNumberRockets << std::endl;
			file << "SystemScreenWidth " << SystemScreenWidth << std::endl;
			file << "SystemScreenHeight " << SystemScreenHeight << std::endl;
			file << "SystemColorDepth " << SystemColorDepth << std::endl;
			file << "ExplosionAlphaStart " << ExplosionAlphaStart << std::endl;
			file << "RocketSpawnDelay " << RocketSpawnDelay << std::endl;
			file << "BonusRocketSpawnDelay " << BonusRocketSpawnDelay << std::endl;
			file << "DualRocketSpawnDelay " << DualRocketSpawnDelay << std::endl;
			file << "RocketVelocity " << RocketVelocity << std::endl;
			file << "BonusRocketVelocity " << BonusRocketVelocity << std::endl;
			file << "DualRocketVelocity " << DualRocketVelocity << std::endl;
			file << "ShowCombosAfterAmount " << ShowCombosAfterAmount << std::endl;
			file << "RocketScore " << RocketScore << std::endl;
			file << "BonusRocketScore " << BonusRocketScore << std::endl;
			file << "DualRocketScore " << DualRocketScore << std::endl;
			file << "StartingTime " << StartingTime << std::endl;
			file << "TimeWarning1 " << TimeWarning1 << std::endl;
			file << "TimeWarning2 " << TimeWarning2 << std::endl;
			file << "MultiplierHighlight1 " << MultiplierHighlight1 << std::endl;
			file << "MultiplierHighlight2 " << MultiplierHighlight2 << std::endl;
			file << "RocketsRemainingWarning1 " << RocketsRemainingWarning1 << std::endl;
			file << "RocketsRemainingWarning2 " << RocketsRemainingWarning2 << std::endl;
			file << "MultiplierAddition " << MultiplierAddition << std::endl;
			file << "MultiplierDecreasePerSecond " << MultiplierDecreasePerSecond << std::endl;
			file << "MultiplierRocketFirePenalty " << MultiplierRocketFirePenalty << std::endl;
			file << "MultiplierPenaltyMin " << MultiplierPenaltyMin << std::endl;
			file << "ScoreFile " << ScoreFile << std::endl;
			file << "SettingsFile " << SettingsFile << std::endl;
			file << "ShowSplash " << ShowSplash << std::endl;
			file << "SplashTime " << SplashTime << std::endl;
			file << "VolumeMusic " << VolumeMusic << std::endl;
			file << "VolumeSound " << VolumeSound << std::endl;

			file.close();

		}
	}
}